package com.xiaoxu.intranetweb.mybatisplusSzappdb.business.compete;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.log.SysLogBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.dao.UserInfoDao;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.SysUser;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.TCompete;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.TCompeteDetail;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.TCompeteTender;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ICCompanysService;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ICProductsInfoService;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ITCompeteService;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ITCompeteTenderService;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.impl.TCompeteDetailServiceImpl;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.impl.TCompeteServiceImpl;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.GetCompeteDetailVo;
import com.xiaoxu.intranetweb.util.SzResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class CompeteDetailBu {
    @Autowired
    private TCompeteDetailServiceImpl tCompeteDetailService;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private SysLogBu sysLogBu;
    @Autowired
    private TCompeteServiceImpl tCompeteServiceImpl;
    @Autowired
    private ITCompeteService itCompeteService;
    @Autowired
    private ICCompanysService icCompanysService;
    @Autowired
    private ICProductsInfoService icProductsInfoService;
    @Autowired
    private ITCompeteTenderService itCompeteTenderService;

    //查询招标详情表
/*    public SzResult getCompeteDetail(String competeId) {
        QueryWrapper<TCompeteDetail> tCompeteDetailQueryWrapper = new QueryWrapper<>();
        tCompeteDetailQueryWrapper.eq(competeId !=null && !competeId.equals(""),"COMPETE_ID", competeId);
        tCompeteDetailQueryWrapper.eq("DEL_FLAG", 0);
        List<TCompeteDetail> list = tCompeteDetailService.list(tCompeteDetailQueryWrapper);
        return SzResult.success(list);
    }*/
    public SzResult getCompeteDetail(String id, String competeId, String plantId) {
        QueryWrapper<TCompeteDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("DEL_FLAG", 1);
        queryWrapper.eq(id != null && !id.equals(""), "ID", id);
        queryWrapper.eq(competeId != null && !competeId.equals(""), "COMPETE_ID", competeId);
        queryWrapper.eq("DATA_FROM", plantId);
        List<TCompeteDetail> list = tCompeteDetailService.list(queryWrapper);
        List<GetCompeteDetailVo> getCompeteDetailVos = new ArrayList<>();
        //将competeDetailList里的每个数据的pdtId拿出来，查询productsInfo表的pdtinf_pdt，然后放到getCompeteDetailVos里，getCompeteDetailVos的其他字段也从competeDetailList里拿
        for (TCompeteDetail tCompeteDetail : list) {
            GetCompeteDetailVo getCompeteDetailVo = new GetCompeteDetailVo();
            getCompeteDetailVo.setId(tCompeteDetail.getId());
            getCompeteDetailVo.setCompeteId(tCompeteDetail.getCompeteId());
            getCompeteDetailVo.setPdtId(tCompeteDetail.getPdtId());
            getCompeteDetailVo.setPdtWeight(tCompeteDetail.getPdtWeight());
            getCompeteDetailVo.setPdtPrice(tCompeteDetail.getPdtPrice());
            getCompeteDetailVo.setMinBid(tCompeteDetail.getMinBid());
            getCompeteDetailVo.setDelFlag(tCompeteDetail.getDelFlag());
            getCompeteDetailVo.setPdtName(icProductsInfoService.getById(tCompeteDetail.getPdtId()).getPdtinfPdt());
            getCompeteDetailVos.add(getCompeteDetailVo);
        }
        return SzResult.success(getCompeteDetailVos);
    }

    //新增招标详情表（competeId必传）

    public SzResult addCompeteDetail(String competeId, String pdtId, String pdtWeight, String pdtPrice, String minBid, String token, String plantId) throws Exception {
//根据competeId和pdtId查询招标详情表，如果有数据，不可新增
        QueryWrapper<TCompeteDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("COMPETE_ID", competeId);
        queryWrapper.eq("PDT_ID", pdtId);
        queryWrapper.eq("DATA_FROM", plantId);
        queryWrapper.ne("DEL_FLAG",1);
        TCompeteDetail tCompeteDetail2 = tCompeteDetailService.getOne(queryWrapper);
        if (tCompeteDetail2 != null) {
            return SzResult.error("该产品已存在，不可新增");
        }
        QueryWrapper<TCompete> tCompeteQueryWrapper1 = new QueryWrapper<>();
        tCompeteQueryWrapper1.eq("ID", competeId);
        tCompeteQueryWrapper1.eq("DATA_FROM", plantId);
        TCompete tCompete1 = tCompeteServiceImpl.getOne(tCompeteQueryWrapper1);
        if (tCompete1.getBeginTDate().isBefore(LocalDateTime.now())) {
            return SzResult.error("已到标段开始时间，不可新增！");
        }
        TCompeteDetail tCompeteDetail = new TCompeteDetail();
        if (competeId != null && !competeId.equals("")) {
            tCompeteDetail.setCompeteId(competeId);
        }
        if (pdtId != null && !pdtId.equals("")) {
            tCompeteDetail.setPdtId(pdtId);
        }
        if (pdtWeight != null && !pdtWeight.equals("")) {
            tCompeteDetail.setPdtWeight(new BigDecimal(pdtWeight));
        }
        if (pdtPrice != null && !pdtPrice.equals("")) {
            tCompeteDetail.setPdtPrice(new BigDecimal(pdtPrice));
        }
        if (minBid != null && !minBid.equals("")) {
            tCompeteDetail.setMinBid(Double.parseDouble(minBid));
        }
        tCompeteDetail.setDelFlag(0);
        tCompeteDetail.setDataFrom(plantId);
        tCompeteDetailService.save(tCompeteDetail);
        SysUser user1 = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        //根据competeId查询招标表的title

        String title = tCompete1.getTitle();
        sysLogBu.logRecord("新增竞标详情信息", user1.getTrueName(), "新增了主题为【" + title + "】的竞标的详情信息", plantId);
        return SzResult.Success();

    }

    //删除招标详情表
    public SzResult delCompeteDetail(String id, String plantId) {
        QueryWrapper<TCompeteDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);
        queryWrapper.eq("DATA_FROM", plantId);
        TCompeteDetail tCompeteDetail = tCompeteDetailService.getOne(queryWrapper);
        TCompete tCompete = itCompeteService.getById(tCompeteDetail.getCompeteId());
//        if (tCompete.getStatus() > 20) {
//            return SzResult.error("状态大于20，不可删除");
//        }
        if (tCompete.getBeginDate().compareTo(LocalDateTime.now()) < 0) {
            return SzResult.error("已过投标开始时间，不可删除！");
        }
        Duration duration = java.time.Duration.between(tCompete.getBeginTDate(), LocalDateTime.now());//开始时间
        Duration duration1 = java.time.Duration.between(tCompete.getBeginDate(), LocalDateTime.now());//结束时间
        if (duration.toMinutes() >= 0 && duration1.toMinutes() < 0) {
            return SzResult.error("投标时间正在开始，不可删除！");
        }
        if (duration.toMinutes() > -5 && duration1.toMinutes() < 0) {
            return SzResult.error("距起投时间不足5分钟，不可删除！");
        }
        if (duration.toMinutes() >= 0 && duration1.toMinutes() >= 0) {
            return SzResult.error("投标时间已截止，不可删除！");
        }
        QueryWrapper<TCompeteDetail> competeDetailQueryWrapper = new QueryWrapper<>();
        competeDetailQueryWrapper.eq("ID", id);
        competeDetailQueryWrapper.eq("DATA_FROM", plantId);
        TCompeteDetail competeDetail = tCompeteDetailService.getOne(competeDetailQueryWrapper);
        competeDetail.setDelFlag(1);
        tCompeteDetailService.updateById(competeDetail);
        return SzResult.Success();
    }

    //修改招标详情表
/*    public SzResult updateCompeteDetail(String id, String competeId, String pdtId, String pdtWeight, String pdtPrice, String minBid) {
        TCompeteDetail tCompeteDetail = new TCompeteDetail();

        tCompeteDetail.setCompeteId(competeId);
        tCompeteDetail.setPdtId(pdtId);


        tCompeteDetail.setMinBid(Double.parseDouble(minBid));
        boolean update = tCompeteDetailService.updateById(tCompeteDetail);
        if (update) {
            return SzResult.success("修改成功");
        }
        return SzResult.error("修改失败");
    }*/

    public SzResult updateCompeteDetail(String id, String competeId, String pdtId, String pdtWeight, String pdtPrice, String minBid, String token, String plantId) throws Exception {
        TCompete tCompete = itCompeteService.getById(competeId);
        QueryWrapper<TCompeteTender> tCompeteTenderQueryWrapper = new QueryWrapper<>();
        tCompeteTenderQueryWrapper.eq("COMPETE_ID", competeId);
        tCompeteTenderQueryWrapper.eq("DATA_FROM", plantId);
        List<TCompeteTender> tCompeteTenderList = itCompeteTenderService.list(tCompeteTenderQueryWrapper);
        if (!tCompeteTenderList.isEmpty()) {
            return SzResult.error("已有投标，不可修改！");
        }
        if (tCompete.getBeginDate().compareTo(LocalDateTime.now()) < 0) {
            return SzResult.error("投标时间已截止，不可修改！");
        }
        Duration duration = java.time.Duration.between(tCompete.getBeginTDate(), LocalDateTime.now());//开始时间
        Duration duration1 = java.time.Duration.between(tCompete.getBeginDate(), LocalDateTime.now());//结束时间
        if (duration.toMinutes() >= 0 && duration1.toMinutes() < 0) {
            return SzResult.error("已过投标开始时间，不可修改！");
        }
        if (duration.toMinutes() > -5 && duration1.toMinutes() < 0) {
            return SzResult.error("距起投时间不足5分钟，不可修改！");
        }
        if (duration.toMinutes() >= 0 && duration1.toMinutes() >= 0) {
            return SzResult.error("投标时间已截止，不可修改！");
        }
        QueryWrapper<TCompeteDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("DEL_FLAG", "0");
        queryWrapper.eq(id != null && !id.equals(""), "ID", id);
        queryWrapper.eq(competeId != null && !competeId.equals(""), "COMPETE_ID", competeId);
        queryWrapper.eq("DATA_FROM", plantId);
        TCompeteDetail tCompeteDetail = tCompeteDetailService.getOne(queryWrapper);
        TCompeteDetail tCompeteDetail1 = new TCompeteDetail();
        tCompeteDetail1.setId(tCompeteDetail.getId());
        if (pdtId != null && !pdtId.equals("")) {
            tCompeteDetail1.setPdtId(pdtId);
        }
        if (pdtWeight != null && !pdtWeight.equals("")) {
            tCompeteDetail1.setPdtWeight(new BigDecimal(pdtWeight));
        }
        if (pdtPrice != null && !pdtPrice.equals("")) {
            tCompeteDetail1.setPdtPrice(new BigDecimal(pdtPrice));
        }
        if (minBid != null && !minBid.equals("")) {
            tCompeteDetail1.setMinBid(Double.valueOf(minBid));
        }
        tCompeteDetailService.updateById(tCompeteDetail1);
        SysUser user1 = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        //根据competeId查询招标表的title
        QueryWrapper<TCompete> tCompeteQueryWrapper1 = new QueryWrapper<>();
        tCompeteQueryWrapper1.eq("ID", competeId);
        tCompeteQueryWrapper1.eq("DATA_FROM", plantId);
        TCompete tCompete1 = tCompeteServiceImpl.getOne(tCompeteQueryWrapper1);
        String title = tCompete1.getTitle();
        sysLogBu.logRecord("修改竞标详情信息", user1.getTrueName(), "修改了" + title + "的竞标详情信息", plantId);
        return SzResult.Success();
    }
}
